<template>
	<view>
		<view class="cu-form-group">
			<view class="title">{{lang == 'en' ? 'Title' : '反馈标题'}}</view>
			<input type="text" v-model="bean.title" :placeholder="lang == 'cn' ? '请输入标题' : 'Please input title.'"/>
		</view>
		<view class="padding-sm margin-top bg-white">
			<textarea :placeholder="lang == 'cn' ? '请输入反馈内容' : 'Please enter content.'" v-model="bean.content" />
		</view>
		
		<view class="cu-form-group margin-top">
			<view class="title">{{lang == 'cn' ? '反馈图片' : 'Image' }}</view>
			<button class="cu-btn bg-cyan" @click="to_choose_image">{{lang == 'cn' ? '上传图片' : 'Upload Image'}}</button>
		</view>
		
		<view class="padding-sm text-center" v-if="bean.feedbackImg">
			<image class="radius" :src="bean.feedbackImg" mode="aspectFit"></image>
		</view>
		
		<view class="" style="height: 60px;">
		</view>
		<view class="cu-bar tabbar bg-default foot">
			<view class="bg-orange submit" @click="save_bean">{{lang == 'cn' ? '确认反馈' : 'Submit Feedback'}}</view>
		</view>
	</view>
</template>

<script>
	// 源码有偿，请加微信 codeal  200￥ 以便养家
	export default{
		data:function(){
			return {
				lang:uni.getStorageSync('lang'),
				
				bean:{}
			}
		},
		methods:{
			to_choose_image:function(){
				uni.chooseImage({
					count:1,
					success: (res) => {
						uni.showLoading({
							title:this.lang == 'cn' ? '上传中' : 'Uploading'
						});
						uni.uploadFile({
							url: this.host + '/tool/uploadImage',
							filePath:res.tempFilePaths[0],
							name:'imgFile',
							success: (res) => {
								var result = JSON.parse(res.data);
								if(result.code == 200){
									this.$set(this.bean,'feedbackImg',result.data.url);
								}
								uni.hideLoading();
							}
						});
					}
				})
			},
			save_bean:function(){
				if(!uni.getStorageSync('user')){
					this.util.toast(this.lang == 'cn' ? '请登录后操作' : 'Please log in.');return;
				}else{
					this.bean.userId = uni.getStorageSync('user').id;
					this.bean.nickname = uni.getStorageSync('user').nickname;
					this.bean.userIcon = uni.getStorageSync('user').icon;
					this.bean.email = uni.getStorageSync('user').email;
					this.bean.lang = uni.getStorageSync('lang');
				}
				
				if(!this.bean.title){
					this.util.toast(this.lang == 'cn' ? '请填写标题' : 'Plase input title.');return;
				}
				if(!this.bean.content){
					this.util.toast(this.lang == 'cn' ? '请输入内容' : 'Please input content.');return;
				}
				if(!this.bean.feedbackImg){
					this.util.toast(this.lang == 'cn' ? '请上传图片' : 'Please upload image');return;
				}
				
				uni.request({
					url:this.host + '/today/saveUserFeedback',
					method:'POST',
					header:this.util.header(),
					data:this.bean,
					success: (res) => {
						if(res.data.data.flag == 0){
							setTimeout(() =>{
								uni.navigateBack({})
							},1500);
						}
						this.util.toast(res.data.data.msg);
					}
				})
			}
		},
		onShow() {
			this.lang = uni.getStorageSync('lang');
		}
	}
</script>

<style>
</style>
